<template>
  <el-dialog
    v-model="visible"
    :title="mode === 'create' ? '新增会议室' : '编辑会议室'"
    width="600px"
  >
    <el-form :model="form" label-width="120px" ref="formRef">
      <el-form-item label="会议室名称" prop="room_name" required>
        <el-input v-model="form.room_name" />
      </el-form-item>
      
      <el-form-item label="会议室类型" prop="type" required>
        <el-radio-group v-model="form.type">
          <el-radio-button label="classroom">教室型</el-radio-button>
          <el-radio-button label="roundtable">圆桌型</el-radio-button>
        </el-radio-group>
      </el-form-item>
      
      <el-form-item label="面积(㎡)" prop="area" required>
        <el-input-number v-model="form.area" :min="10" :step="5" />
      </el-form-item>
      
      <el-form-item label="座位数" prop="seats" required>
        <el-input-number v-model="form.seats" :min="1" />
      </el-form-item>
      
      <el-form-item label="设备" prop="equipment">
        <el-checkbox-group v-model="form.equipment">
          <el-checkbox label="projector">投影仪</el-checkbox>
          <el-checkbox label="audio">音响</el-checkbox>
          <el-checkbox label="network">网络</el-checkbox>
          <el-checkbox label="whiteboard">白板</el-checkbox>
          <el-checkbox label="video_conference">视频会议</el-checkbox>
          <el-checkbox label="telephone">电话</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      
      <el-form-item label="价格(元/小时)" prop="price" required>
        <el-input-number v-model="form.price" :min="0" :precision="2" />
      </el-form-item>
    </el-form>
    
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确认</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { useAuthStore } from '@/stores/auth'

const authStore = useAuthStore()
const props = defineProps({
  modelValue: Boolean,
  room: Object,
  mode: String
})

const emit = defineEmits(['update:modelValue', 'submit'])

const formRef = ref(null)
const visible = ref(false)
const form = ref({
  room_name: '',
  type: 'classroom',
  area: 0,
  seats: 0,
  equipment: [],
  price: 0,
  room_id: 0
})

const resetForm = () => {
  form.value = {
    room_name: '',
    type: 'classroom',
    area: 0,
    seats: 0,
    equipment: [],
    price: 0,
    room_id: 0
  }
}

watch(() => props.modelValue, (val) => {
  visible.value = val
})

watch(visible, (val) => {
  emit('update:modelValue', val)
})

watch(() => props.room, (val) => {
  if (val) {
    form.value = {
      room_name: val.room_name,
      type: val.type,
      area: val.area,
      seats: val.seats,
      equipment: Array.isArray(val.equipment) ? val.equipment : JSON.parse(val.equipment || '[]'),
      price: val.price,
      room_id: val.room_id
    }
  } else {
   resetForm()
  }
}, { immediate: true })

const handleSubmit = async () => {
  try {
    await formRef.value.validate()
    const userId = authStore.getUserId
    const data = {
      ...form.value,
      equipment: form.value.equipment,
      userId
    }
    emit('submit', data)
    visible.value = false
  } catch (error) {
    console.log(error)
    ElMessage.warning('请填写完整信息')
  }
}
</script>

<style scoped>
.el-checkbox {
  margin-right: 15px;
}
</style>